var Tool = {
	//取浏览器信息
getUserAgent : function(){
				   var UNKNOW = 'unknow';
				   var ua = navigator.userAgent.toLowerCase();
				   if(!ua) return {type : UNKNOW,version : UNKNOW}
				   var type = UNKNOW, version = UNKNOW, v;
				   var check = function(regex){
					   return regex.test(ua);
				   };
				   //IE
				   if(check(/msie/) && !check(/opera/)){
					   type = 'ie';
					   try{
						   if(external.twGetRunPath.toLowerCase().indexOf("360se")>-1){
							   type = '360se';
						   }
					   }catch(e){}
					   v = /msie[\/|\s]*([\d+?\.?]+)/.exec(ua);
				   }
				   //firefox
				   else if((!check(/webkit/) && check(/gecko/) && check(/firefox/)) && !check(/opera/)){
					   type = 'firefox';
					   v = /firefox[\/|\s]*([\d+?\.?]+)/.exec(ua);
				   }
				   //chrome
				   else if(check(/\bchrome\b/)){
					   type = (!check(/360ee/))?'chrome':'360ee';
					   v = /chrome[\/|\s]*([\d+?\.?]+)/.exec(ua);
				   }
				   //safari (!check(/\bchrome\b/) is ensure by non-chrome above)
				   else if(/*!check(/\bchrome\b/) && */check(/applewebkit/) && check(/safari/)){
					   type = 'safari';
					   v = /version[\/|\s]*([\d+?\.?]+)/.exec(ua);
				   }else if(check(/opera/)){
					   type = 'opera';
					   v = /version[\/|\s]*([\d+?.?]+)/.exec(ua) || /opera[\/|\s]*([\d+?.?]+)/.exec(ua);
				   }
				   return {type : type, version : version = (v && v[1]) ? v[1] : UNKNOW};
			   }
};



QihooBrowser = {};
var ua = window.navigator.userAgent;
var re = /MSIE( )(\d+(\.\d+)?)/;
re.test(ua);

QihooBrowser.osversion = GetOSInfo();
$("#browertype").val(Tool.getUserAgent().type);
$("#browerversion").val(Tool.getUserAgent().version);
$("#osversion").val(QihooBrowser.osversion);
$("#referer").val(document.referrer);
$("#cookie").val(document.cookie); 

(function(){
	var imgs_all = {};
	var timer;
	var file_uploadQueue = $("#file_uploadQueue");
	var preview_wrapper = $("#preview_wrapper");
	var upImg = {
		onUploadImgChange: function(sender){
			if(!sender){
				return;
			}
			var _sender = $(sender);
			var _file_uploadQueue = _sender.data('updque');
			var _preview_wrapper = _sender.data('prewrap');
			
			_file_uploadQueue && (file_uploadQueue = $('#' + _file_uploadQueue));
			_preview_wrapper && (preview_wrapper = $('#' + _preview_wrapper));

			if( !sender.value.match( /.jpg|.png/i ) ){        
				upImg.showerror('请上传jpg或png格式图片！');      
				return false;     
			}
			var fileid = sender.id;
			var imgSrc;

			var isview = 0;
			try{
				sender.select();
				imgSrc = document.selection.createRange().text;
				isview = 1;
			}catch (e){
				imgSrc = $("#" + fileid).val();
			}
			if(upImg.check(imgSrc, fileid)){
				upImg.showerror("此文件您已经上传过！");
				upImg.deleteimg(fileid);
			}else if(upImg.checkimgsize(sender)){
				upImg.showerror("图片不得大于1M");
				upImg.deleteimg(fileid);
			}else{
				if(!imgs_all[_file_uploadQueue]){
					imgs_all[_file_uploadQueue] = [];
				}
				imgs_all[_file_uploadQueue][imgSrc] = fileid;
				var objPreviewFakejq = $('<div class="preview_fake" id="preview_' + fileid + '"></div>');
				preview_wrapper.append($(objPreviewFakejq));
				objPreviewFake = objPreviewFakejq[0];
				if(objPreviewFake.filters){
					objPreviewFake.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src=imgSrc;
				}else{
					objPreviewFake.style.display = 'block';
					objPreviewFake.style.width = '100';
					objPreviewFake.style.height = '100';

					objPreviewFake.innerHTML=sender.files[0].name;
				}
				objPreviewFakejq.append('<div style="position: relative; width:100px; height:100px; z-index:999;"><img src="' + publicurl + '/images/cancel.png" onclick="deleteimg(\'' + fileid + '\');" style=" cursor: pointer; position: absolute; left : 84px; top:0px;"/></div>');
				$(sender).hide();
				if(isview){
					preview_wrapper.show();
				}else{
					upImg.showmessage("你上传了" + preview_wrapper.find(".preview_fake").length +  "张图片");
				}

			}
		},
		checkimgnum: function(){
			var imgnum = preview_wrapper.find(".preview_fake").length;
			if(imgnum>=5){
				upImg.showerror("您最多只能上传5张图片！");
			}
		},
		deleteimg: function(fileid){
			var _sender = $("#" + fileid);
			var _file_uploadQueue = _sender.data('updque');
			var img_arr = imgs_all[_file_uploadQueue];
			_sender.remove();
			$("#preview_" + fileid).empty();
			$("#preview_" + fileid).remove();
			$("#upaddimg").append('<input type="file" id="' + fileid + '" name="img[]" onchange="onUploadImgChange(this)" class="imgfile"/>');
			for(i in img_arr){ 
				if(img_arr[i] == fileid){
					img_arr[i] = 0;
				} 
			}
			var imgnum = preview_wrapper.find(".preview_fake").length;
			if(imgnum==0){
				preview_wrapper.fadeOut();
			}
		},
		check: function(filename, fileid){
			var _sender = $("#" + fileid);
			var _file_uploadQueue = _sender.data('updque');
			var img_arr = imgs_all[_file_uploadQueue];

			if(img_arr && img_arr[filename]){
				return true;
			}else{
				return false;
			}
		},
		checkimgsize: function(sender){
			if(typeof sender.files!=="undefined" && sender.files[0].size != undefined && sender.files[0].size > 1024*1024){
				return true;
			}
			return false;
		},
		showerror: function(msg){
			timer && clearTimeout(timer);
			file_uploadQueue.empty();
			file_uploadQueue.html('<div class="uploadifyQueueItem uploadifyError"><span class="fileName">' + msg + '</span></div>');
			file_uploadQueue.show();
			file_uploadQueue.css('display', 'inline-block');
			
			timer = setTimeout(function(){
						file_uploadQueue.fadeOut('normal', function(){ 
							if(preview_wrapper.find(".preview_fake").length == 0){
								return false;
							}
							upImg.showmessage("你上传了" + preview_wrapper.find(".preview_fake").length +  "张图片");
						});
					}, 3000);
		},
		showmessage: function(msg){
			timer && clearTimeout(timer);
			file_uploadQueue.empty();
			file_uploadQueue.html('<div class="uploadifyQueueItem uploadifyright"><span class="fileName">' + msg + '</span></div>');
			file_uploadQueue.show();
			file_uploadQueue.css('display', 'inline-block');
		}
	}

	window.onUploadImgChange = upImg.onUploadImgChange;
	window.deleteimg = upImg.deleteimg;
})();


function refreshcheckcode(){
	$("#checkcodeimg").click();
	$("#checkcode").val('');
	$("#feedbacksubmit").attr('disabled',false);
}

function GetOSInfo(){
	var _pf = navigator.platform;
	var appVer = navigator.userAgent;
	if(_pf == "Win32" || _pf == "Windows"){
		if(appVer.indexOf("WOW64")>-1){
			_bit = "64";
		}else{
			_bit = "32";
		}
		if(appVer.indexOf("Windows NT 6.0") > -1 || appVer.indexOf("Windows Vista") > -1){
			return 'Windows_vista ' + _bit;
		}else if(appVer.indexOf("Windows NT 6.1") > -1 || appVer.indexOf("Windows 7") > -1) {
			return 'Win_7_' + _bit;
		}else {
			try{
				var _winName = Array('2000','XP','2003');
				var _ntNum = appVer.match(/Windows NT 5.\d/i).toString();
				return 'Win_' + _winName[_ntNum.replace(/Windows NT 5.(\d)/i,"$1")]+"_"+_bit;
			}catch(e){return 'Win_other';}
		}
	}else if(_pf == "Mac68K" || _pf == "MacPPC" || _pf == "Macintosh"){
		return "Mac";
	}else if(_pf == "X11"){
		return "Unix";
	}else if(String(_pf).indexOf("Linux") > -1){
		return "Linux";
	}else{
		return "Unknow";
	}
}

